package com.feige.utils.mybatis;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * @author feige<br />
 * @ClassName: MybatisUtil <br/>
 * @Description: <br/>
 * @date: 2021/8/23 10:15<br/>
 */
public class MybatisUtil {

    public static <T> QueryWrapper<T> wrapper(WrapperObj... args){
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        for (WrapperObj wrapperObj : args) {
            Object value = wrapperObj.getValue();
            if (value instanceof String){
                if (StrUtil.isBlank((String) value)){
                    continue;
                }
            } else {
                if (ObjectUtil.isNull(value)){
                    continue;
                }
            }
            switch (wrapperObj.getType()){
                case EQ:
                    wrapper.eq(wrapperObj.getName(), value);
                    break;
                case GE:
                    wrapper.ge(wrapperObj.getName(), value);
                    break;
                case GT:
                    wrapper.gt(wrapperObj.getName(), value);
                    break;
                case IN:
                    wrapper.in(wrapperObj.getName(), value);
                    break;
                case LE:
                    wrapper.le(wrapperObj.getName(), value);
                    break;
                case LT:
                    wrapper.lt(wrapperObj.getName(), value);
                    break;
                case NE:
                    wrapper.ne(wrapperObj.getName(), value);
                    break;
                case LIKE:
                    wrapper.like(wrapperObj.getName(), value);
                    break;
            }
        }
        return wrapper;
    }
}
